package com.tt.service.impl;

import com.tt.entity.bo.MapBO;
import com.tt.entity.vo.MapVO;
import com.tt.mapper.DepartmentApparentTotalMapper;
import com.tt.service.MapService;
import com.tt.util.HandleDecimal;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * <p>
 *
 * </p>
 *
 * @version 1.0
 * @auther sugarttt
 * @since 2022-11-10 10:33:45
 */
@Service
public class MapServiceImpl implements MapService {

    private final DepartmentApparentTotalMapper departmentApparentTotalMapper;

    public MapServiceImpl(DepartmentApparentTotalMapper departmentApparentTotalMapper) {
        this.departmentApparentTotalMapper = departmentApparentTotalMapper;
    }

    @Override
    public ArrayList<MapVO> getMap(String beginYear, String endYear) {
        int diff = Integer.parseInt(endYear) - Integer.parseInt(beginYear) + 1;
        ArrayList<MapVO> mapVOS = new ArrayList<>();


        ArrayList<MapBO> mapBOS = departmentApparentTotalMapper.getMap(beginYear, endYear);
        Iterator<MapBO> iterator = mapBOS.iterator();

        MapBO mapBO;
        double sumValue = 0.0;
        for (int id=diff; iterator.hasNext();){
            mapBO = iterator.next();

            if (id <= diff) {
                sumValue += Double.parseDouble(mapBO.getValue());
            }

            --id;

            if (id == 0) {
                mapVOS.add(new MapVO(mapBO.getName(), mapBO.getProvinceId(), HandleDecimal.handle(sumValue)));

                // sum next province value of each year
                sumValue = 0.0;
                id = diff;
            }
        }
        return mapVOS;
    }
}
